package com.jshop.admin.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jshop.admin.sys.entity.SysRoleEntity;
import com.jshop.admin.sys.service.SysRoleDeptService;
import com.jshop.admin.sys.service.SysRoleMenuService;
import com.jshop.admin.sys.service.SysRoleService;
import com.jshop.admin.sys.service.SysUserService;
import com.jshop.common.constant.AdminConstant;
import com.jshop.common.result.CommonResult;
import com.jshop.common.utils.Query;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/sys/sysRole")
public class SysRoleController extends AbstractController {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysRoleDeptService sysRoleDeptService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 角色列表
     */
    @ApiOperation("角色列表")
    @GetMapping("/list")
    public CommonResult<IPage<SysRoleEntity>> list(@RequestParam Map<String, Object> params) {
        QueryWrapper queryWrapper = new QueryWrapper();
        //查询列表数据
        Object keyword = params.get("keyword");
        if (keyword != null & !"".equals(keyword)) {
            queryWrapper.like("role_name", keyword);
        }
        IPage page = new Query(params).getPage();
        IPage<SysRoleEntity> sysRoleEntityIPage = sysRoleService.page(page,queryWrapper);
        return CommonResult.successData(sysRoleEntityIPage);
    }

    /**
     * 角色列表
     */
    @ApiOperation("角色列表-去除超管和商户角色的接口")
    @GetMapping("/select")
    public CommonResult<List<SysRoleEntity>> select() {
        //查询普通管理员角色
        List<SysRoleEntity> list = sysRoleService.queryPublicRole();
        return CommonResult.successData(list);
    }

    @GetMapping("listAll")
    public CommonResult listAll() {
        List<SysRoleEntity> list = sysRoleService.list();
        return CommonResult.successData(list);

    }

    /**
     * 角色信息
     */
    @ApiOperation("角色信息")
    @GetMapping("/info/{roleId}")
    public CommonResult<SysRoleEntity> info(@PathVariable("roleId") Long roleId) {
        SysRoleEntity role = sysRoleService.getById(roleId);
        QueryWrapper queryWrapper = new QueryWrapper();
        //查询角色对应的菜单
        queryWrapper.eq("role_id", roleId);

        List<Integer> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
        role.setMenuIdList(menuIdList);

        return CommonResult.successData(role);
    }

    /**
     * 保存角色
     */
    @ApiOperation("保存角色")
    @PostMapping("/save")
    public CommonResult save(@RequestBody SysRoleEntity role) {
        role.setCreateUserId(getUserId());
        sysRoleService.insertInto(role);
        return CommonResult.successMsg("操作成功");
    }

    /**
     * 修改角色
     */
    @ApiOperation("修改角色")
    @PostMapping("/update")
//    @RequiresPermissions("sys:role:update")
    public CommonResult update(@RequestBody SysRoleEntity role) {
        Long roleId = role.getRoleId();
        if (roleId == AdminConstant.MERCHANT || roleId == AdminConstant.COMMON_ADMIN || roleId == AdminConstant.SUPER_ADMIN) {
            return CommonResult.failed("关键角色不能修改");
        }
        role.setCreateUserId(getUserId());
        sysRoleService.updateInfo(role);
        return CommonResult.successMsg("操作成功");
    }

    /**
     * 删除角色
     */
    @ApiOperation("删除角色")
    @PostMapping("/delete")
//    @RequiresPermissions("sys:role:delete")
    public CommonResult delete(@RequestBody Long[] roleIds) {
        QueryWrapper queryWrapper;
        List deleteId = new ArrayList();
        for(Long roleId : roleIds) {
            if (roleId == AdminConstant.MERCHANT || roleId == AdminConstant.COMMON_ADMIN || roleId == AdminConstant.SUPER_ADMIN) {
                return CommonResult.successMsg("关键角色不能删除");
            }
            deleteId.add(roleId);
        }
        sysRoleService.removeByIds(deleteId);
        return CommonResult.successMsg("操作成功");
    }

    @GetMapping("data")
    public CommonResult data() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.apply("TO_DAYS( NOW( ) ) - TO_DAYS( create_time) <= 1");
        int yesterday = sysRoleService.count(queryWrapper);
        queryWrapper = new QueryWrapper<>();
        queryWrapper.apply("to_days(create_time) = to_days(now())");
        //今天
        int dayCount = sysRoleService.count(queryWrapper);
        queryWrapper = new QueryWrapper();
        //7天内
        queryWrapper.apply("DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(create_time)");
        int day7 = sysRoleService.count(queryWrapper);
        queryWrapper = new QueryWrapper();
        queryWrapper.apply("DATE_SUB(CURDATE(),  INTERVAL 1 MONTH) <= date(create_time)");
        //最近一个月
        int day30 = sysRoleService.count(queryWrapper);
        queryWrapper = new QueryWrapper();
        queryWrapper.apply("DATE_SUB(CURDATE(),  INTERVAL 3 MONTH) <= date(create_time)");
        //最近三个月
        int day90 = sysRoleService.count(queryWrapper);
        //总数量
        int count = sysRoleService.count();

        Map map = new HashMap();
        map.put("dayCount", dayCount);
        map.put("day7", day7);
        map.put("day30", day30);
        map.put("day90", day90);
        map.put("count", count);
        map.put("yesterday", yesterday);
        return CommonResult.successData(map);

    }
}
